<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-col :span="12">
          <a-form-item label="学生pin" v-bind="validateInfos.pin">
            <a-input v-model:value="formData.pin" placeholder="请输入学生pin" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="主叫号码" v-bind="validateInfos.billcaller">
            <a-input v-model:value="formData.billcaller" placeholder="请输入主叫号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="被叫号码" v-bind="validateInfos.billcalled">
            <a-input v-model:value="formData.billcalled" placeholder="请输入被叫号码" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="号码类别" v-bind="validateInfos.calltype">
	          <a-input-number v-model:value="formData.calltype" placeholder="请输入号码类别" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="通话类型" v-bind="validateInfos.billcalltype">
            <a-input v-model:value="formData.billcalltype" placeholder="请输入通话类型" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="通话开始" v-bind="validateInfos.start">
            <a-input v-model:value="formData.start" placeholder="请输入通话开始" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="通话结束" v-bind="validateInfos.end">
            <a-input v-model:value="formData.end" placeholder="请输入通话结束" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="通话时长，单位秒" v-bind="validateInfos.duration">
	          <a-input-number v-model:value="formData.duration" placeholder="请输入通话时长，单位秒" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="话费" v-bind="validateInfos.phonebill">
            <a-input v-model:value="formData.phonebill" placeholder="请输入话费" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="创建时间" v-bind="validateInfos.createtime">
            <a-input v-model:value="formData.createtime" placeholder="请输入创建时间" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="学生姓名" v-bind="validateInfos.realname">
            <a-input v-model:value="formData.realname" placeholder="请输入学生姓名" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="卡号" v-bind="validateInfos.cardid">
            <a-input v-model:value="formData.cardid" placeholder="请输入卡号" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="设备SN" v-bind="validateInfos.devicesn">
            <a-input v-model:value="formData.devicesn" placeholder="请输入设备SN" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="扣费状态 扣费状态:未扣费，扣费失败，已扣费" v-bind="validateInfos.deductionstatus">
            <a-input v-model:value="formData.deductionstatus" placeholder="请输入扣费状态 扣费状态:未扣费，扣费失败，已扣费" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="学校组织ID" v-bind="validateInfos.organizationid">
	          <a-input-number v-model:value="formData.organizationid" placeholder="请输入学校组织ID" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="班级/部门id" v-bind="validateInfos.departmentid">
	          <a-input-number v-model:value="formData.departmentid" placeholder="请输入班级/部门id" style="width: 100%" :disabled="disabled"/>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
  import { ref, reactive, defineExpose, nextTick, defineProps, computed, onMounted } from 'vue';
  import { defHttp } from '/@/utils/http/axios';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getValueType } from '/@/utils';
  import { saveOrUpdate } from '../SsTpaiPhoneRecord.api';
  import { Form } from 'ant-design-vue';
  
  const props = defineProps({
    formDisabled: { type: Boolean, default: false },
    formData: { type: Object, default: ()=>{} },
    formBpm: { type: Boolean, default: true }
  });
  const formRef = ref();
  const useForm = Form.useForm;
  const emit = defineEmits(['register', 'ok']);
  const formData = reactive<Record<string, any>>({
    id: '',
    pin: '',   
    billcaller: '',   
    billcalled: '',   
    calltype: undefined,
    billcalltype: '',   
    start: '',   
    end: '',   
    duration: undefined,
    phonebill: '',   
    createtime: '',   
    realname: '',   
    cardid: '',   
    devicesn: '',   
    deductionstatus: '',   
    organizationid: undefined,
    departmentid: undefined,
  });
  const { createMessage } = useMessage();
  const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 5 } });
  const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 16 } });
  const confirmLoading = ref<boolean>(false);
  //表单验证
  const validatorRules = {
    billcaller: [{ required: true, message: '请输入主叫号码!'},],
    billcalled: [{ required: true, message: '请输入被叫号码!'},],
    calltype: [{ required: true, message: '请输入号码类别!'},],
    billcalltype: [{ required: true, message: '请输入通话类型!'},],
    start: [{ required: true, message: '请输入通话开始!'},],
    end: [{ required: true, message: '请输入通话结束!'},],
    duration: [{ required: true, message: '请输入通话时长，单位秒!'},],
    phonebill: [{ required: true, message: '请输入话费!'},],
    createtime: [{ required: true, message: '请输入创建时间!'},],
    realname: [{ required: true, message: '请输入学生姓名!'},],
    devicesn: [{ required: true, message: '请输入设备SN!'},],
    deductionstatus: [{ required: true, message: '请输入扣费状态 扣费状态:未扣费，扣费失败，已扣费!'},],
    organizationid: [{ required: true, message: '请输入学校组织ID!'},],
  };
  const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

  // 表单禁用
  const disabled = computed(()=>{
    if(props.formBpm === true){
      if(props.formData.disabled === false){
        return false;
      }else{
        return true;
      }
    }
    return props.formDisabled;
  });

  
  /**
   * 新增
   */
  function add() {
    edit({});
  }

  /**
   * 编辑
   */
  function edit(record) {
    nextTick(() => {
      resetFields();
      //赋值
      Object.assign(formData, record);
    });
  }

  /**
   * 提交数据
   */
  async function submitForm() {
    // 触发表单验证
    await validate();
    confirmLoading.value = true;
    const isUpdate = ref<boolean>(false);
    //时间格式化
    let model = formData;
    if (model.id) {
      isUpdate.value = true;
    }
    //循环数据
    for (let data in model) {
      //如果该数据是数组并且是字符串类型
      if (model[data] instanceof Array) {
        let valueType = getValueType(formRef.value.getProps, data);
        //如果是字符串类型的需要变成以逗号分割的字符串
        if (valueType === 'string') {
          model[data] = model[data].join(',');
        }
      }
    }
    await saveOrUpdate(model, isUpdate.value)
      .then((res) => {
        if (res.success) {
          createMessage.success(res.message);
          emit('ok');
        } else {
          createMessage.warning(res.message);
        }
      })
      .finally(() => {
        confirmLoading.value = false;
      });
  }


  defineExpose({
    add,
    edit,
    submitForm,
  });
</script>

<style lang="less" scoped>
  .antd-modal-form {
    height: 500px !important;
    overflow-y: auto;
    padding: 14px;
  }
</style>
